<template>
  <view class="activity-detail">
    <!-- 顶部图片 -->
    <view class="activity-banner">
      <cover-image :src="url(activity?.image, '', 'png')" mode="aspectFill" class="banner-image"></cover-image>
      <view class="banner-overlay">
        <view class="activity-date">
          <text class="month">{{ activity?.month }}</text>
          <text class="year">{{ activity?.year }}</text>
        </view>
      </view>
    </view>

    <!-- 活动内容 -->
    <view class="activity-content">
      <view class="activity-header">
        <text class="activity-title">{{ activity?.title }}</text>
        <!-- <button 
        :type="isSignedUp ? 'default' : 'warn'" 
        class="signup-btn" 
        @tap="handleSignUp"
        :disabled="isSignedUp"
      >
        <text class="bi bi-calendar-check me-2"></text>
        {{ isSignedUp ? '您已预约' : '立即预约' }}
      </button> -->
      </view>

      <view class="activity-info">
        <text class="info-title">活动详情</text>
        <text class="info-content">{{ activity?.fullDesc }}</text>
      </view>

      <!-- 活动须知 -->
      <view class="activity-notice">
        <text class="notice-title">活动须知</text>
        <view class="notice-list">
          <view class="notice-item">
            <text class="bi bi-check-circle-fill text-success"></text>
            <text>请提前10分钟到达活动地点</text>
          </view>
          <view class="notice-item">
            <text class="bi bi-check-circle-fill text-success"></text>
            <text>活动期间请遵守场地规则</text>
          </view>
          <view class="notice-item">
            <text class="bi bi-check-circle-fill text-success"></text>
            <text>请携带身份证等有效证件</text>
          </view>
          <view class="notice-item">
            <text class="bi bi-check-circle-fill text-success"></text>
            <text>如需取消请提前24小时告知</text>
          </view>
        </view>
      </view>
    </view>


    <view class="footer-bar">
      <button 
        :type="isSignedUp ? 'default' : 'warn'" 
        class="signup-btn" 
        @tap="handleSignUp"
        :disabled="isSignedUp"
      >
        <text class="bi bi-calendar-check me-2"></text>
        {{ isSignedUp ? '您已预约' : '立即预约' }}
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import { useUserStore } from '@/stores/userStore';

const url = $url;
const activity = ref(null);
const userStore = useUserStore();

// 计算是否已预约
const isSignedUp = computed(() => {
  if (!activity.value || !userStore.userInfo?.activity) return false;
  return userStore.userInfo.activity.includes(activity.value.id);
});

onMounted(() => {
  // 从本地存储获取活动数据
  const activityData = uni.getStorageSync('currentActivity');
  if (activityData) {
    activity.value = activityData;
  }
});

// 处理预约
const handleSignUp = async () => {
  if (isSignedUp.value) return;
  
  try {
    uni.showLoading({
      title: '预约中...'
    });
    
    // 构建用户活动数组
    const updatedActivity = [...(userStore.userInfo?.activity || []), activity.value.id];
    
    // 调用云函数更新用户信息
    const { result } = await uniCloud.callFunction({
      name: 'updateUserInfo',
      data: {
        body: JSON.stringify({
          action: "updateUser",
          account: userStore.userInfo.account,
          activity: updatedActivity  // 直接在根级别传递activity字段
        })
      }
    });
    
    if (result && result.status === 1) {
      // 更新本地用户信息
      userStore.updateUserInfo({
        ...userStore.userInfo,
        activity: updatedActivity
      });
      
      uni.showToast({
        title: '预约成功',
        icon: 'success'
      });
    } else {
      throw new Error(result?.message || '预约失败');
    }
  } catch (error) {
    console.error('预约失败:', error);
    uni.showToast({
      title: error.message || '预约失败，请重试',
      icon: 'none'
    });
  } finally {
    uni.hideLoading();
  }
};
</script>

<style lang="scss" scoped>
.activity-detail {
  min-height: 100vh;
  background: #f8f8f8;
  padding-bottom: 120rpx;
}

.activity-banner {
  position: relative;
  height: 400rpx;
  
  .banner-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  
  .banner-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(to bottom, rgba(0,0,0,0.2), rgba(0,0,0,0.6));
    display: flex;
    align-items: flex-end;
    padding: 40rpx;
  }
  
  .activity-date {
    background: var(--td-brand-color, #981f23);
    padding: 20rpx 40rpx;
    border-radius: 12rpx;
    color: #fff;
    text-align: center;
    
    .month {
      font-size: 36rpx;
      font-weight: bold;
      display: block;
    }
    
    .year {
      font-size: 24rpx;
      opacity: 0.9;
    }
  }
}

.activity-content {
  background: #fff;
  border-radius: 40rpx 40rpx 0 0;
  margin-top: -40rpx;
  padding: 40rpx;
  position: relative;
  z-index: 1;
}

.activity-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40rpx;
  
  .activity-title {
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
  }
}

.activity-info {
  margin-bottom: 40rpx;
  
  .info-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 20rpx;
    display: block;
  }
  
  .info-content {
    font-size: 28rpx;
    color: #666;
    line-height: 1.8;
    white-space: pre-line;
  }
}

.activity-notice {
  background: #f8f8f8;
  padding: 30rpx;
  border-radius: 16rpx;
  
  .notice-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 20rpx;
    display: block;
  }
  
  .notice-list {
    display: flex;
    flex-direction: column;
    gap: 20rpx;
  }
  
  .notice-item {
    display: flex;
    align-items: center;
    gap: 10rpx;
    font-size: 26rpx;
    color: #666;
    
    .bi {
      font-size: 32rpx;
    }
  }
}

.footer-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  padding: 20rpx 40rpx;
  box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.05);
  
  .signup-btn {
    width: 100%;
    
    &[disabled] {
      background-color: #f5f5f5;
      color: #999;
    }
  }
}
</style> 